package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.*;
import com.clkj.xiuzheng.pojo.*;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author yawang
 * @create 2024/04/11 13:43:41
 **/
@Service
public class ShopServiceImpl implements ShopService{

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SettingMapper settingMapper;
    @Autowired
    private UserPutMapper userPutMapper;
    @Autowired
    private UserShopApplyMapper userShopApplyMapper;

    private Map map;
    @Autowired
    private ShopVisitMapper shopVisitMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Map shopList(Integer shopRegion) {

        List<Shop> shops =shopMapper.listShopsByNewId(shopRegion);
        map = new HashMap();
        map.put("shops", shops);
        return map;
    }

    @Override
    public Map searchShop(String shopRegions, String shopTargets, String shopLevels) {

        String [] regions = shopRegions.split(",");
        String  [] target = shopTargets.split(",");
        String []level = shopLevels.split(",");

        Example example = new Example(Shop.class);
        Example.Criteria criteria = example.createCriteria();
        if(shopRegions.length()>0){
            criteria.andIn("shopRegion", Arrays.asList(regions));
        }

        if(shopLevels.length()>0){
            criteria.andIn("shopLevel", Arrays.asList(level));
        }

        Example.Criteria criteria1 = example.createCriteria();
        if(shopTargets.length()>0){
            for (int i = 0; i < target.length; i++) {
                criteria1.orLike("shopTarget", "%"+target[i]+ ",%");
            }
            example.and(criteria1);
        }
        map = new HashMap();
        map.put("shops",shopMapper.selectByExample(example));

        return map;
    }
    @Override
    public Map listMain() {
        List<Shop> shopsNum = shopMapper.listShopNum();
        map = new HashMap();
        map.put("shops", shopsNum);
        map.put("shops-msg","shops表示各个区域的门店数量,shopRegion表示区域，shopDayNum表示数量");

        List<Shop> shopAdv = shopMapper.listShopAdv();
        map.put("shopAdv", shopAdv);
        map.put("shopAdv-msg","shopAdv表示各个区域已投放广告的店铺数量,shopRegion表示区域，shopDayNum表示数量");

        List<Shop> shopUnAdv = shopMapper.listShopUnAdv();
        map.put("shopUnAdv", shopUnAdv);
        map.put("shopUnAdv-msg","shopUnAdv表示各个区域未投放广告的店铺数量,shopRegion表示区域，shopDayNum表示数量");

        List<Shop> ShopAdvNum = shopMapper.listShopAdvNum();
        map.put("ShopAdvNum", ShopAdvNum);
        map.put("ShopAdvNum-msg","ShopAdvNum表示各个区域投放广告的数量,shopRegion表示区域，shopDayNum表示数量");


        List<Shop> AdvNum = shopMapper.listAdvNum();
        map.put("AdvNum", AdvNum);
        map.put("AdvNum-msg","AdvNum广告商总数");


        return map;
    }

    @Override
    public void addOrUpdateSetting(Setting setting) {
        if(setting.getSettingId()==null){
            settingMapper.insertSelective(setting);
        }else{
            settingMapper.updateByPrimaryKeySelective(setting);
        }

    }

    @Override
    public Setting listSetting() {
        Example example = new Example(Setting.class);
        return settingMapper.selectOneByExample(example);
    }

    @Override
    public Map listHcustomer() {
        List<UserPut> userPut = userPutMapper.listHcustomer();
        map = new HashMap<>();
        map.put("userPut", userPut);
        return map;
    }

    @Override
    public Map backCount() {

        return null;
    }

    @Override
    public PageInfo listCommission(Integer pageSize, Integer pageNum, Integer applySataus, Integer userId) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserShopApply> userShopApplies = userShopApplyMapper.listCommission(applySataus,userId);
        return new PageInfo<>(userShopApplies);
    }

    @Override
    public List<UserShopApply> MylistCommission(Integer userId) {
        Example example = new Example(UserShopApply.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        List<UserShopApply> list = userShopApplyMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<ShopVisit> MyBalanceList(Integer userId) {
        Example example = new Example(ShopVisit.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("visitApply",1);
        return shopVisitMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public void commitCommission(Integer userId, Double applyMoney) {
        UserShopApply userShopApply = new UserShopApply();
        User user = userMapper.selectByPrimaryKey(userId);
        if (user.getUserBalance()<applyMoney){
            throw new RuntimeException("余额不足");
        }

        userShopApply.setApplyMoney(applyMoney);
        userShopApply.setApplyDate(DateFormatHelper.currentTimes());
        userShopApply.setApplyStatus(0);
        userShopApply.setUserId(userId);
        user.setUserBalance(user.getUserBalance()-applyMoney);
        userMapper.updateByPrimaryKeySelective(user);
        userShopApplyMapper.insertSelective(userShopApply);

    }

    @Override
    public void passCommission(Integer applyId) {
        UserShopApply userShopApply= new UserShopApply();
        userShopApply.setApplyId(applyId);
        userShopApply.setApplyStatus(1);
        userShopApplyMapper.updateByPrimaryKeySelective(userShopApply);
    }

    @Override
    public PageInfo getPutList(Integer pageSize, Integer pageNum, Integer isPut) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserPut> list =  userPutMapper.getPutList(isPut);
        return new PageInfo<>(list);
    }
}
